<template>
	<view>
		<!-- 顶部导航部分 -->
		<carHeader class="blue" :title="'卡券添加'"></carHeader>


		<!-- 卡片部分 -->
		<view class="nr">
			<uni-card>
				<!-- 优惠名称部分 -->
				<view class="titletime">
					<text class="title">优惠券名称</text>
					<input v-model="couName"></input>
				</view>

				<!-- 优惠券类型 -->
				
				<view class="youhuititle" ref="form">
					<text class="title">优惠券类型</text>
					<view class="type" v-if="type == 'full_reduce'">
						<view class="yi">
							<text class="one">满减券</text>

							<image src="../../static/车联网服务商家端-07卡券管理3-发放优惠券_slices/组 6@2x.png"></image>
							<text class="two">打折券</text>
							<div @click="changeShow1">
								<image src="../../static/车联网服务商家端-07卡券管理3-发放优惠券_slices/椭圆 3 拷贝@2x.png"></image>
							</div>

						</view>

						<view class="right">
							满<input v-model="couTriggerMoney" />元减<input v-model="couPreferentialMoney" />元
						</view>
					</view>
					<view class="type" v-if="type == 'discount'">
						<view class="yi">
							<text class="one">满减券</text>
							<div @click="changeShow2">
								<image src="../../static/车联网服务商家端-07卡券管理3-发放优惠券_slices/椭圆 3 拷贝@2x.png"></image>
							</div>
							<text class="two">打折券</text>
							<image src="../../static/车联网服务商家端-07卡券管理3-发放优惠券_slices/组 6@2x.png"></image>
						</view>

						<view class="right">
							满<input v-model="couTriggerMoney" />元打<input v-model="couDiscount" />折
						</view>

					</view>

				</view>

				<!-- 发放开发时间 -->
				<view class="time">
					<view class="open_end_time">发放开始时间</view>
					<view class="selecttime">
						<picker mode="date" @change="ffks">
							<view>{{startDate}}</view>
						</picker>
						<image src="/static/gengduojiantou.png"></image>
					</view>
				</view>
				<view class="time">
					<view class="open_end_time">发放结束时间</view>
					<view class="selecttime">
						<picker mode="date" @change="ffjs">
							<view>{{endDate}}</view>
						</picker>
						<image src="/static/gengduojiantou.png"></image>
					</view>
				</view>


				<!-- 投放数量 -->
				<view class="number">
					<view class="youhuititle1">
						<text class="title1">投放数量</text>
					</view>
					<input v-model="couNum" /><text style="color: black;">张</text>
				</view>
				<view class="number">
					<view class="youhuititle1">
						<text class="title1">每人限领</text>
					</view>
					<input v-model="couSize" /><text style="color: black;">张</text>
				</view>


				<!-- 使用有效期 -->
				<view class="youxiaoqi">
					<view class="youhuititle1">
						<text class="title1">使用有效期</text>
					</view>
					<view class="selecttime1">
						<picker mode="date" @change="start">
							<view>{{startdate}}</view>
						</picker>
						<image src="/static/gengduojiantou.png"></image>
					</view>

					<view class="selecttime1" style="margin-left: 10rpx;">
						<picker mode="date" @change="end">
							<view>{{enddate}}</view>
						</picker>
						<image src="/static/gengduojiantou.png"></image>
					</view>
				</view>
			</uni-card>
			<!-- 保存按钮 -->
			<view class="save" @click="add">
				确认投放
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				couName:'',
				type: 'full_reduce',//full_reduce
				couTriggerMoney: '', //触发金额
				couPreferentialMoney: '', //优惠金额
				couDiscount: '', //折扣金额
				couNum: '',
				couSize: '',
				show1: true,
				/* 发放优惠券的开始时间和结束时间 */
				startDate: this.getDate(),
				endDate: this.getDate(),
				/* 使用有效期的开始时间和结束时间 */
				startdate: this.getDate(),
				enddate: this.getDate()
			}
		},

		methods: {

			changeShow1() {
				// if (this.type != '满减') {
				// 	this.type = '满减';
				// }
				this.type = 'discount';
			},
			changeShow2() {
				// if (this.type != '打折') {
				// 	this.type = '打折';
				// }
				// this.type = '满减';
				this.type = 'full_reduce';
			},
			getDate() {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			ffks(e) {
				this.startDate = e.detail.value
			},
			ffjs(e) {
				this.endDate = e.detail.value
			},
			start(e) {
				this.startdate = e.detail.value
			},
			end(e) {
				this.enddate = e.detail.value
			},
			add() {
				var that = this;
				uni.request({
					url: "http://localhost:8080/carShop/couponszh/couponsinsert",
					data: {
						couponName: this.couName,
						couponType: this.type,
						triggerMoney: this.couTriggerMoney,
						preferentialMoney: this.couPreferentialMoney,
						discount: (this.couDiscount * 0.01),
						/* 发放优惠券的开始时间和结束时间 */
						start: this.startDate,
						end: this.enddate,
						/* 使用有效期的开始时间和结束时间 */
						expirationStart: this.startdate,
						expirationEnd: this.enddate,
						num: this.couNum,
						limitReceive: this.couSize,
						status: 'active'
					},
					method: 'POST',
					success(res) {
						console.log(res);
						uni.showToast({
							title: "投放成功",
							icon: 'success',
							duration: 2000
						})
						console.log(res)
						that.couName = null,
							that.type = 'full_reduce',
							that.couTriggerMoney = null,
							that.couPreferentialMoney = null,
							that.couDiscount = null,
							that.startDate = that.getDate(),
							that.endDate = that.getDate(),
							that.startdate = that.getDate(),
							that.enddate = that.getDate(),
							that.couNum = null,
							that.couSize = null
					}
				})
			}
		}
	}
</script>

<style>
	.nr {

		position: relative;
		top: -100px
	}


	/* 优惠名称样式 */
	.titletime {
		display: flex;
		color: #151515;
		line-height: 88rpx;
		align-items: center;
	}

	.titletime .title {
		font-weight: bold;
		width: 150rpx;
	}

	.titletime>input {
		margin-left: 65rpx;
		width: 400rpx;
		height: 56rpx;
		border: 1rpx solid #c3c3c3;
		border-radius: 29rpx;
		padding-left: 13rpx;
		padding-right: 13rpx;
		text-align: center;
	}

	/* 优惠类型 */
	.youhuititle {
		display: flex;
		color: #151515;
		line-height: 88rpx;
	}

	.youhuititle .title {
		font-weight: bold;
		width: 150rpx;
	}

	.type {
		display: flex;
		flex-direction: column;
		margin-top: 23rpx;
		margin-left: 70rpx;
	}

	.yi {
		display: flex;
		align-items: center;
	}

	.two {
		margin-left: 60rpx;
	}

	.yi image {
		margin-left: 10rpx;
		width: 30rpx;
		height: 30rpx;
	}

	.right {
		height: 54rpx;
		display: flex;
	}

	.right input {
		height: 100%;
		border-bottom: 1rpx solid black;
		width: 120rpx;
		margin-left: 4rpx;
		text-align: center;
	}

	/* 发放开始和结束时间样式 */
	.time {
		display: flex;
		margin-top: 60rpx;
		align-items: center;
	}

	.open_end_time {
		font-weight: bold;
		width: 180rpx;
		color: black;
		margin-right: 40rpx;
	}

	.selecttime {
		display: flex;
		align-items: center;
		width: 260rpx;
		height: 65rpx;
		text-align: center;
		border: 1rpx solid #c3c3c3;
		border-radius: 29rpx;
		color: #c3c3c3;
		padding-left: 12rpx;
	}

	.selecttime image {
		padding-left: 60rpx;
		width: 19rpx;
		height: 17rpx;
	}



	/* 投放数量样式 */
	.number {
		display: flex;
		align-items: center;
		margin-top: 50rpx;
	}

	.number input {
		border: 1rpx solid #d0d0d0;
		width: 100rpx;
		height: 29rpx;
		border-radius: 20rpx;
		margin-right: 8rpx;
		text-align: center;
	}

	.youhuititle1 .title1 {
		font-weight: bold;
		width: 150rpx;
		color: #151515;
		margin-right: 110rpx;
	}


	/* 使用有效期的样式 */
	.youxiaoqi {
		display: flex;
		align-items: center;
		margin-top: 39rpx;
	}

	.selecttime1 {
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 153rpx;
		height: 65rpx;
		text-align: center;
		border: 1rpx solid #c3c3c3;
		border-radius: 29rpx;
		color: #c3c3c3;
		padding-left: 10rpx;
		font-size: 25rpx;
		margin-right: 20rpx;
	}

	.selecttime1 image {
		/* padding-left: 25rpx; */
		width: 19rpx;
		height: 17rpx;
	}

	.youxiaoqi .selecttime1 {
		margin-left: -30rpx;
	}




	/* 确认投放按钮样式 */
	.save {
		line-height: 79rpx;
		width: 517rpx;
		height: 74rpx;
		border: 1rpx solid white;
		border-radius: 30rpx;
		text-align: center;
		margin-left: 120rpx;
		margin-top: 30rpx;
		color: white;
		background-color: #1b6fea;
	}
</style>